package cn.rongdao.jrkabao.ui.message;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashEmptyUIComponent;
import cn.rongdao.jrkabao.ui.message.adapter.SystemMessageAdapter;
import cn.rongdao.jrkabao.ui.message.adapter.SystemMessageViewHolder;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;

/**
 * Created by jack on 2017/8/21
 */

public abstract class BaseMessageActivity extends BaseAutoFinishActivity implements FRBaseRecyclerAdapter.OnItemClickListener<SystemMessageViewHolder> {

    @BindView(R.id.abm_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.abm_ptrframelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    @BindView(R.id.abm_empty_view)
    protected CashEmptyUIComponent cashEmptyUIComponent;

    protected SystemMessageAdapter mAdapter;

    @Override
    protected int layoutRes() {
        return R.layout.activity_base_message;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitleBar();
        initPtr();
    }

    private void initPtr() {
        mAdapter = new SystemMessageAdapter(this, this);
        ptrRecyclerViewUIComponent.setBackground(R.color.layout_bg);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(this));
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);
        ptrRecyclerViewUIComponent.delayRefresh();
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getMessageData();
            }
        });
    }

    protected abstract void getMessageData();

    protected abstract String getTitleName();

    private void initTitleBar() {
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initTitle(getTitleName());
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void runUI(int flag) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
        switch (flag) {
            case 0:
                ptrRecyclerViewUIComponent.setVisibility(View.GONE);
                cashEmptyUIComponent.initEmptyView("暂时还没有消息哦！");
                break;
            case 1:
                ptrRecyclerViewUIComponent.setVisibility(View.VISIBLE);
                cashEmptyUIComponent.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onItemClick(SystemMessageViewHolder systemMessageViewHolder, int position) {
        if (!FRString.isEmpty(mAdapter.getDataList().get(position).getJump_url())) {
            WebViewActivity.start(thisActivity(), mAdapter.getDataList().get(position).getJump_url());
            return;
        }
        MessageDetailsActivity.start(thisActivity(), mAdapter.getDataList().get(position).getMessage_id());
    }
}
